The Art of Deja Vu - Implementing 'for' loops in C -------------------------------------------------- The concept of the 'loop' is one of the fundamental requirements in any language. In most advanced languages, (and 'C' is no exception) there are three types of loops, the WHILE...DO, the REPEAT...UNTIL, and the FOR...NEXT loop. The tradition basic for loop looks something like, 10 FOR x = 0 TO 20 STEP 5 20 do some stuff... 30 NEXT x The same loop in C would look like, int x; for (x=0;x<=20;x=x+5) { do some stuff... } A little fast?, lets get into some detail, a FOR...NEXT loop requires three things, an initialzer (where to start), a terminator or condition (where to end), and a step or increment (what to do after each pass of the loop). The general structure of a 'C' for loop is; for ( INITIALIZATION ; CONDITION or TEST ; INCREMENT ) { /* do stuff here */ } So the initialization, or 'x=1' sets the variable x to the value 0, the condition tests to see that x is less than or equal to the value 20, and the increment says to increase x by the value 5 after each pass. Notice that the basic NEXT keyword is missing?, that's because the closing brace '}' implies the next condition. Is that brace necessary? No, if you want to do just one thing, say set every fifth value of an array called Stuff[] to 0 then you could use these lines, int Stuff[21]; int x; for (x=0;x<=20;x=x+5) Stuff[x] = 0; Now the closeing semicolon after the '0' implies the next statement. In fact if we wanted to clear out the entire array we could do it ALL within the brakets of the for loop, by using the post increment operator ++ as in 'x++' which means the same as 'x = x+1' we can write out for loop like this, int Stuff[21]; int x; for (x=0;x<=20;Stuff[x++]=0); Now the semicolon after closing braket implies the NEXT command. Here the semicolon is called the NULL operator because it is saying 'Do nothing in this loop'. Now what of our original problem, zeroing every fifth element of the array? This can also be done entirely within the brakets using the comma operator like this, int Stuff[21] int x; for (x=0;x<=20;Stuff[x]=0,x=x+5); The comma operator allows us to perform several operations at the same step. This can be used anywhere in a 'C' program although sometimes it is neccessary to enclose the commands in brakets, ie (Stuff[21]=0,x=x+5) in order to prevent confusion as to what the operator does. This is especialy useful in the for loop however, for instance if to variable need to be initiallized the comma operator can be used like, int x,y; for (x=0,y=20;x<20;x++); Now lets look at the condition, the condition in 'C' should produce some form of logical response, that is a zero value or a non-zero value. As soon as the condition evaluates to false (ie zero) the loop will end. This means that a piece of code like, int x; for (x=20;x;x--) { printf("\n%d",x); /* print out the value of x on a new line */ } Will print the numbers from 20 to 1 then quit, because when x reaches zero, the condition evaluates to false and the loop ends. Furthermore there is no rule which says you MUST include each of the three parts of the loop. This means that the example above is functionally equivalent to, int x; x = 20; for (;x;x--) printf("\n%d",x); In which the initializer has been left out, or even, int x; for (x=20;--x;) printf("\n%d",x); Where the increment field has been left out. The for loop also has to features in C not found in the familiar FOR...NEXT loops of BASIC, these are the keywords 'break' and 'continue'. The format for using a 'break' command is as follows, for ( initializer ; condition ; increment ) { /* do stuff ... */ if ( condition2 ) break; /* do other stuff ... */ } /* do stuff outside loop */ What the break command does is force the for loop to end, right then and there. So in the above general format if the program evaluates 'condition2' to be true (ie non-zero) then the break instruction is executed causing execution of the program to skip to the 'stuff outside loop' without executing the 'other stuff'. The 'continue' command works slightly differently, it's format is, for ( initializer ; condition ; increment ) { /* do stuff ... */ if ( condition2 ) continue; /* do other stuff ... */ } The way this command works is to force the loop to go to the NEXT command that is if condition2 evaluates true, the continue command will force the loop to perform the increment and condition steps of the loop as if it had reached the end of the loop, but without ever executing the 'do other stuff' portion of the code. An analogy in BASIC would be, 10 FOR x = 1 to 10 20 rem -- do stuff -- 30 if condition2 goto 50: rem -- this is the 'continue' 40 rem -- do other stuff -- 50 NEXT x The rest of this tutorial is made up of some code fragments showing uses of 'C' for loops. /****************************************** convert long int to zero padded ascii, the value in num is NOT preserved *******************************************/ #define MAX_DIGITS 9 long num; int x; char *string = " "; /* string is MAX_DIGITS long */ for ( x=MAX_DIGITS ; num/=10 ; *(string+x--)= num%10+'0'); /* end of conversion */ /*********************************************** count the number of records in a linked list ************************************************/ /* note: "node->next" indicates next node in list */ struct node *root; /* pointer to first node in list */ struct node *work; /* temp pointer */ int records; for ( work=root,records=0 ; work ; records++,work=work->next ); /* end of count, records now contains # of elements, work is NULL (ie 0) */ /*********************************************** find a matching password in a list ***********************************************/ /* notes: "node->next" indicates next element in list, "node->pswd" is a pointer to a string of characters */ struct node *root,*work; char *pswd; /* assumed to already point to the input password */ for (work=root; work; ) { if (strcmp(pswd,work->pswd)==0) break; work = work->next; } /* work now points to the matching node for the supplied password, or work will now equal NULL if the password was not found */ Thus concludes our attempts at chasing ourselves in 'C' using the for loop. Any questions? You can direct them at me Stephen Orr on this board until about Sept. 9 after which I'll be out of town for 4 months! I hope it's been useful! Bye for now. Stephen Orr